package com.prd.controller;

import com.prd.constant.CheckStatus;
import com.prd.dto.SupervisorVo;
import com.prd.model.EntTask;
import com.prd.model.EntTaskCheck;
import com.prd.model.EntTaskStage;
import com.prd.service.EntTaskService;
import com.prd.util.AjaxResult;
import com.prd.util.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "任务管理")
@RestController
@RequestMapping("/api/entTask")
public class EntTaskController extends BaseController {

    @Autowired
    private EntTaskService entTaskService;

    @ApiOperation("查询任务列表")
    @GetMapping("list")
    public TableDataInfo list(@ApiParam("日期") String day, @ApiParam("1:我发起、2：我接收、3：执行中") Integer type) {
        startPage();
        List<EntTask> list = entTaskService.listInfo(day, type);
        return getDataPage(list);
    }

    @ApiOperation("添加任务")
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody EntTask entTask) {
        return toAjax(entTaskService.insertInfo(entTask));
    }

    @ApiOperation("修改任务")
    @PostMapping(value = "update")
    public AjaxResult update(@RequestBody EntTask entTask) {
        return toAjax(entTaskService.updateInfo(entTask));
    }

    @ApiOperation("查询任务详情")
    @GetMapping(value = "details/{id}")
    public AjaxResult details(@PathVariable("id") Long id) {
        return AjaxResult.success(entTaskService.details(id));
    }

    @ApiOperation("查询任务人员权限")
    @GetMapping("jurisdiction/{taskId}")
    public AjaxResult jurisdiction(@PathVariable("taskId") Long taskId) {
        return AjaxResult.success(entTaskService.jurisdiction(taskId));
    }

    @ApiOperation("审核通过")
    @PostMapping(value = "pass")
    public AjaxResult pass(@RequestBody EntTaskCheck entTaskCheck) {
        entTaskCheck.setType(CheckStatus.TWO.getCode());
        return toAjax(entTaskService.pass(entTaskCheck));
    }

    @ApiOperation("审核不通过")
    @PostMapping(value = "noPass")
    public AjaxResult noPass(@RequestBody EntTaskCheck entTaskCheck) {
        entTaskCheck.setType(CheckStatus.THREE.getCode());
        return toAjax(entTaskService.pass(entTaskCheck));
    }

    @ApiOperation("由我负责执行")
    @PostMapping(value = "execute/{taskId}")
    public AjaxResult execute(@PathVariable("taskId") Long taskId) {
        return toAjax(entTaskService.execute(taskId));
    }

    @ApiOperation("任务责任分解")
    @PostMapping(value = "executeResolve")
    public AjaxResult executeResolve(@RequestBody EntTaskStage entTaskStage) {
        return toAjax(entTaskService.executeResolve(entTaskStage));
    }

    @ApiOperation("查询任务不分期执行汇报")
    @GetMapping("listExecutiveNoStage/{taskId}")
    public AjaxResult listExecutiveNoStage(@PathVariable("taskId") Long taskId) {
        return AjaxResult.success(entTaskService.listExecutiveNoStage(taskId));
    }

    @ApiOperation("查询任务分期执行汇报")
    @GetMapping("listExecutiveStage/{stageId}")
    public AjaxResult listExecutiveStage(@PathVariable("stageId") Long stageId) {
        return AjaxResult.success(entTaskService.listExecutiveStage(stageId));
    }

    @ApiOperation("添加任务分解责任")
    @PostMapping(value = "addDecompose")
    public AjaxResult addDecompose(@RequestBody EntTask entTask) {
        return toAjax(entTaskService.addDecompose(entTask));
    }

    @ApiOperation("查询任务分解责任列表")
    @GetMapping("listDecompose/{decompose}/{taskId}")
    public AjaxResult list(@ApiParam("分解责任第几层") Integer decompose, @ApiParam("任务ID") @PathVariable("taskId") Long taskId) {
        return AjaxResult.success(entTaskService.listDecompose(decompose, taskId));
    }

    @ApiOperation("变更任务责任人")
    @PostMapping(value = "updateDuty")
    public AjaxResult updateDuty(@RequestBody EntTask entTask) {
        if (entTask.getId() == null || StringUtils.isBlank(entTask.getReasons()) || StringUtils.isBlank(entTask.getOverall())) {
            return AjaxResult.error("参数不能为空！");
        }
        return toAjax(entTaskService.updateDuty(entTask));
    }

    @ApiOperation("添加督导审核结果")
    @PostMapping(value = "addSupervisor")
    public AjaxResult addSupervisor(@RequestBody SupervisorVo supervisorVo) {
        return toAjax(entTaskService.addSupervisor(supervisorVo));
    }

    @ApiOperation("撤销任务")
    @PostMapping(value = "revocation/{taskId}")
    public AjaxResult revocation(@PathVariable("taskId") Long taskId) {
        return toAjax(entTaskService.revocation(taskId));
    }

    @ApiOperation("增加抄送人")
    @PostMapping(value = "addMake/{taskId}/{makes}")
    public AjaxResult addMake(@ApiParam("任务ID") @PathVariable("taskId") Long taskId, @ApiParam("抄送人（多个,分开）") @PathVariable("makes") Long[] makes) {
        return toAjax(entTaskService.addMake(taskId, makes));
    }
}
